﻿using AntdUI;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using WenAntdUI.Helps;
using WenAntdUI.Settings;
using Button = AntdUI.Button;

namespace WenAntdUI.Controls;

[ToolboxItem(true)]
public class DownloadFile : IControl
{
    #region 私有属性
    private readonly Input input;
    private readonly Button button;
    private readonly Progress progress;
    #endregion
    public DownloadFile() : base()
    {
        input = new Input()
        {
            Dock = DockStyle.Fill,
            JoinRight = true,
            BorderWidth = 1,
        };

        this.Controls.Add(input);

        progress = new Progress()
        {
            Padding = new Padding(5, 0, 5, 0),
            Dock = DockStyle.Fill,
            Height = 12,
            Margin = new Padding(0),
            Text = "上传进度",
            Visible = false,
        };
        this.Controls.Add(progress);

        button = new Button()
        {
            IconSvg = "DownloadOutlined",
            BorderWidth = 1,
            Width = 60,
            Dock = DockStyle.Right,
            Text = "下载",
            JoinLeft = true,
        };
        this.Controls.Add(button);

        button.Click += Button_Click; ;
    }

    private async void Button_Click(object? sender, EventArgs e)
    {
        var url = input.Text;
        if (string.IsNullOrEmpty(url))
        {
            Msgbox.ShowError("请输入下载地址");
            return;
        }
        if (Guid.Parse(input.Text) is Guid guid)
        {
            url = $"{AppSettings.GetString("apiurl")}/api/file/{guid}";
        }
        if (!url.StartsWith("http"))
        {
            Msgbox.ShowError("请输入正确的下载地址");
            return;
        }

        if (!Directory.Exists(DownloadFolder))
        {
            Directory.CreateDirectory(DownloadFolder);
        }

        this.Invoke(OnStart);

        var pro = new Progress<double>(a =>
        {
            this.ProgressValue = (float)a / 100;
        });

        var fd = new FileDownloader(Token);
        await fd.DownloadFileAsync(url, p => OnDownloadPathChanged?.Invoke(p) ?? Path.Combine(DownloadFolder, p), pro);
    }

    /// <summary>
    /// 开始事件
    /// </summary>
    public void OnStart()
    {
        input.Visible = false;
        progress.Value = 0;
        progress.Visible = true;
        button.Loading = true;
    }

    /// <summary>
    /// 结束事件
    /// </summary>
    public void OnEnd()
    {
        input.Visible = true;
        progress.Visible = false;
        button.Loading = false;
    }

    #region 公有属性

    public string DownloadFolder { get; set; } = AppSettings.GetString("downloadpath") ?? "Download";

    [Category("Wem"), Description("下载进度"), DefaultValue(0)]
    public float ProgressValue
    {
        get => progress.Value;
        set
        {
            if (value == 1)
            {
                this.Invoke(OnEnd);
            }
            progress.Value = value;
        }
    }

    [Category("功能"), Description("Token"), DefaultValue(null)]
    public string? Token { get; set; }

    [Category("功能"), Description("保存路径"), DefaultValue(null)]
    public Func<string, string>? OnDownloadPathChanged { get; set; }
    #endregion
}
